package zhongbo.java.air.dictionary.service.serverImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.common.util.ObjectIsNullUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirportMapper;
import zhongbo.java.air.dictionary.mapper.AlertZoneMapper;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.model.Dto.AirportPageDto;
import zhongbo.java.air.dictionary.model.Dto.PageDto;
import zhongbo.java.air.dictionary.service.AlertZoneServer;

import java.util.Arrays;

@Service
public class AlertZoneServerImpl implements AlertZoneServer {
    @Autowired
    private AlertZoneMapper alerZoneMapper;

    //增加机场信息
    @Override
    public void add(AlertZone alertZone){
        boolean b = ObjectIsNullUtil.allNonIdFieldsNonNull(alertZone);
        if(!b){
            throw new NoStackLogException("关键属性不能为空");
        }
        if(alertZone.getZoneCode().length()>20){
            throw new NoStackLogException("区域业务编码字符长度超过20");
        }
        if(alertZone.getZoneType().length()>20){
            throw new NoStackLogException("区域类型字符长度超过20");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            // 尝试将字符串解析为通用的 Java 对象
            mapper.readTree(alertZone.getVertices());
        } catch (Exception e) {
            // 若解析过程中抛出异常，说明字符串不是有效的 JSON 格式
            throw new NoStackLogException("区域信息无法json解析");
        }

        alerZoneMapper.insert(alertZone);
    }

    //删除机场信息
    @Override
    public void deleteById(String id){
        if(id==null||id.isEmpty()){
            throw new NoStackLogException("区域信息无法json解析");
        }
        LambdaQueryWrapper<AlertZone> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AlertZone::getId,id);
        alerZoneMapper.delete(lqw);
    }

    //修改机场信息,提供id
    @Override
    public void update(AlertZone alertZone){

        if(alertZone.getId()==null||alertZone.getId().isEmpty()){
            throw new NoStackLogException("id不能为空");
        }

        if(alertZone.getId()!=null&&alertZone.getZoneCode().length()>20){
            throw new NoStackLogException("区域业务编码字符长度超过20");
        }
        if(alertZone.getId()!=null&&alertZone.getZoneType().length()>20){
            throw new NoStackLogException("区域类型字符长度超过20");
        }

        LambdaQueryWrapper<AlertZone> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AlertZone::getId,alertZone.getId());
        alerZoneMapper.update(alertZone,lqw);
    }

    //分页查询，可以排序
    @Override
    public PageVo<AlertZone> selectPage(PageDto airportPageDto){

        boolean get = Arrays.stream(airportPageDto.getClass().getDeclaredMethods())
                .filter(method -> method.getName().startsWith("get"))
                .map(method -> {
                    try {
                        return method.invoke(airportPageDto);
                    } catch (Exception e) {
                        return null;
                    }
                })
                .noneMatch(value -> value == null);

        if(get){
            throw new NoStackLogException("关键属性不能为空");
        }

        QueryWrapper<AlertZone> qw = new QueryWrapper<>();
        qw.orderBy(true,airportPageDto.isAsc(),airportPageDto.getSortField());
        Page<AlertZone> page = new Page<>(airportPageDto.getPageNum(),airportPageDto.getPageSize());
        Page<AlertZone> airportPage = alerZoneMapper.selectPage(page, qw);
        PageVo<AlertZone> airportPageVoa = new PageVo<>();
        airportPageVoa.setPage(airportPage.getPages());
        airportPageVoa.setPageSize(airportPage.getSize());
        airportPageVoa.setPageTotal(airportPage.getTotal());
        airportPageVoa.setRecords(airportPage.getRecords());
        return airportPageVoa;
    }
}
